home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / reader_requests / xgtool / xgtoolset / source / hextransmitter / hextransmitter.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  10KB  |  471 lines

  1. /*
  2.     HXTM: A Bars and Pipes tool simple transmitting MIDI-Hex
  3.  
  4.     © 1998 Alfred Faust
  5.  
  6.     The code was written to compile it with the free GNU-C-compiler of  the
  7.     ADE.  Look  at  the  part  ,  that place the imagedate in chip-RAM. The
  8.     GNU-C-compiler hasn't as yet a keyword like DICE or other compiler  for
  9.     handle this easy.
  10.  
  11.     This code is released  into  the  Public  Domain,  and  may  be  freely
  12.     distributed in its original form.
  13.  
  14.     It is supplied ``as is'', and comes with no warranty. This program code
  15.     was  released  because it might be useful as a starting point for other
  16.     programmers. However, if any damage arises from its use,  the  original
  17.     author will not be held liable.
  18.  
  19.     You are free to use and modify  this  code  to  your  heart's  content,
  20.     provided you acknowledge me as the original author in any code that you
  21.     might distribute which is based largely on this code.
  22.  
  23.     I acknowledge that the design of this tool is  influenced  strongly  by
  24.     the  example code supplied with the Rules for Tools package. However, I
  25.     have made substantial contributions of my own.
  26.  
  27.     Alfred Faust
  28.     J.K.Dax@t-online.de
  29.  
  30.     History:
  31.     1.0 (8.11.98)
  32.     Initial Release.
  33.  
  34.     
  35. */
  36.  
  37. #include "bars.h"
  38. #include <libraries/dos.h>
  39. #include <proto/exec.h>
  40. #include <proto/intuition.h>
  41. #include <proto/graphics.h>
  42. #include <exec/exec.h>
  43. #include <exec/memory.h>
  44. #include <exec/execbase.h>
  45. #include <string.h>
  46. #include <intuition/intuition.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <ctype.h>
  50.  
  51.  
  52. #define HXTM_NAME "Hex-Transmitter"
  53.  
  54. #define HXTM_ID          0x4858542F
  55. #define HXTM_TYPE        (TOOL_NORMAL | TOOL_NOTPAD)
  56.  
  57. #define HXSTR_GADGET 1
  58.  
  59. extern struct ExecBase * SysBase;
  60. UWORD *HXTM=NULL;
  61.  
  62. static char sysexbuffer[50]={'f', '0', 'f', '7'};
  63. static char sysexundobuffer[50]={'f', '0', 'f', '7'};
  64.  
  65. static UWORD HXTMdata[] =
  66. {
  67.     /* Plane 0 */
  68.         0x7FFF,0xFE00,
  69.         0x4000,0x0200,
  70.         0x4F42,0xEA00,
  71.         0x4F5E,0xEA00,
  72.         0x4F5F,0x5A00,
  73.         0x4043,0xBA00,
  74.         0x4F5F,0x5A00,
  75.         0x4F5F,0x5A00,
  76.         0x4F5E,0xEA00,
  77.         0x4F42,0xEA00,
  78.         0x4000,0x0200,
  79.         0x7FFF,0xFE00,
  80.     /* Plane 1 */
  81.         0x0000,0x0200,
  82.         0x3FFF,0xFE00,
  83.         0x37BD,0x7600,
  84.         0x37AF,0x7600,
  85.         0xB7AF,0xAF00,
  86.         0xBFBD,0xDF00,
  87.         0xB7AF,0xAF00,
  88.         0xB7AF,0xAF00,
  89.         0x37AF,0x7600,
  90.         0x37BD,0x7600,
  91.         0x3FFF,0xFE00,
  92.         0x3FFF,0xFE00,
  93.     /* Plane 2 */
  94.         0x0000,0x0000,
  95.         0x0000,0x0000,
  96.         0x1FFF,0xF800,
  97.         0x1FFF,0xF800,
  98.         0x1FFF,0xF800,
  99.         0x1FFF,0xF800,
  100.         0x1FFF,0xF800,
  101.         0x1FFF,0xF800,
  102.         0x1FFF,0xF800,
  103.         0x1FFF,0xF800,
  104.         0x0000,0x0000,
  105.         0x0000,0x0000
  106.     };
  107.  
  108. static struct Image HXTM_image = {
  109.   0,0,
  110.   24 , 12 , 3 ,
  111.   NULL,
  112.   0x1f,0x00,
  113.   NULL
  114. };
  115.  
  116. struct HXTMTool
  117. {
  118.   struct Tool tool;
  119.  
  120.   ULONG HX;
  121. };
  122.  
  123. static struct ToolMaster master;
  124.  
  125. extern struct Functions *functions;
  126.  
  127. static void HXTM_tool_init(struct HXTMTool *tool)
  128. {
  129.   tool->tool.touched = TOUCH_INIT;
  130.  
  131.   tool->HX = 0;
  132. }
  133.  
  134. static struct Event * __saveds processeventcode(struct Event *event)
  135. {
  136.   event->tool = event->tool->next;
  137.   return(event);
  138. }
  139.  
  140.  
  141. /* HXSTR_GADGET gadget. */
  142.  
  143. /* Border for the HXSTR_GADGET */
  144.  
  145. static SHORT HXSTRvectors4[] = {     //black
  146.     -4,11,
  147.     -4,-4,
  148.     247,-4
  149. };
  150.  
  151. static struct Border HXSTRBorder4 = {
  152.     0,0,
  153.     1,0,JAM1,
  154.     3,
  155.     HXSTRvectors4,
  156.     NULL
  157. };
  158.  
  159.  
  160.  
  161. static SHORT HXSTRvectors3[] = {     //black
  162.     -4,11,
  163.     247,11,
  164.     247,-4
  165. };
  166.  
  167. static struct Border HXSTRBorder3 = {
  168.     0,0,
  169.     1,0,JAM1,
  170.     3,
  171.     HXSTRvectors3,
  172.     &HXSTRBorder4
  173. };
  174.  
  175.  
  176. static SHORT HXSTRvectors2[] = {     //white
  177.     -5,11,
  178.     -5,-5,
  179.     247,-5
  180. };
  181.  
  182. static struct Border HXSTRBorder2 = {
  183.     0,0,
  184.     2,0,JAM1,
  185.     3,
  186.     HXSTRvectors2,
  187.     &HXSTRBorder3
  188. };
  189.  
  190. static SHORT HXSTRvectors1[] = {  //white
  191.     -3,10,
  192.     246,10,
  193.     246,-3
  194. };
  195.  
  196. static struct Border HXSTRBorder1 = {
  197.     0,0,
  198.     2,0,JAM1,
  199.     3,
  200.     HXSTRvectors1,
  201.     &HXSTRBorder2
  202. };
  203.  
  204. /* stringgadget */
  205.  
  206. static char sysexbuffer[50];
  207. static char sysexundobuffer[50];
  208.  
  209.  
  210. static struct IntuiText HXSTRText = {
  211.     1,0,JAM1,
  212.     -80,-1,
  213.     NULL,
  214.     "MIDI-Hex:",
  215.     NULL
  216. };
  217.  
  218.  
  219. static struct StringInfo HXSTRStringinfo = {
  220.     sysexbuffer,
  221.     sysexundobuffer,
  222.     0, 50, 0,
  223.     0, 0, 0,
  224.     0, 0,
  225.     NULL,
  226.     NULL,
  227.     NULL
  228. };
  229.  
  230. static struct Gadget HXSTRGadget = {
  231.     NULL,
  232.     95,25,
  233.     240,8,
  234.     GADGHCOMP,
  235.     RELVERIFY,
  236.     STRGADGET,
  237.     (APTR)&HXSTRBorder1,
  238.     NULL,
  239.     &HXSTRText,
  240.     NULL,
  241.     (APTR)&HXSTRStringinfo,
  242.     HXSTR_GADGET,
  243.     NULL
  244. };
  245.  
  246. struct NewWindow HXTMNewWindowStructure1 = {
  247.   75,85,
  248.   370, 50,
  249.   0,6,
  250.   GADGETDOWN+GADGETUP+CLOSEWINDOW,
  251.   WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+ACTIVATE+NOCAREREFRESH,
  252.   &HXSTRGadget,
  253.   NULL,
  254.   "HXTM-Transmitter V 1.0",
  255.   NULL,
  256.   NULL,
  257.   5,5,
  258.   -1,-1,
  259.   CUSTOMSCREEN
  260. };
  261.  
  262.  
  263. static void __saveds send_note(struct HXTMTool *tool, const LONG time, const UBYTE note)
  264. {
  265.   struct NoteEvent *on_event = (struct NoteEvent *)(*functions->allocevent)();
  266.   if (on_event)
  267.     {
  268.       struct NoteEvent *off_event = (struct NoteEvent *)(*functions->allocevent)();
  269.       if (off_event)
  270.     {
  271.       on_event->next     = NULL;
  272.       on_event->type     = EVENT_VOICE;
  273.       on_event->status   = MIDI_NOTEON;
  274.       on_event->value    = note;
  275.       on_event->velocity = 127;
  276.       on_event->time     = time;
  277.       on_event->tool     = tool->tool.next;
  278.       (*functions->qevent)(on_event);
  279.  
  280.       off_event->next   = NULL;
  281.       off_event->type   = EVENT_VOICE;
  282.       off_event->status = MIDI_NOTEOFF;
  283.       off_event->value  = note;
  284.       off_event->time   = time + 100;
  285.       off_event->tool   = tool->tool.next;
  286.       (*functions->qevent)(off_event);
  287.     }
  288.       else
  289.     {
  290.       (*functions->freeevent)(on_event);
  291.     }
  292.     }
  293. }
  294.  
  295.  
  296. static void __saveds sendsysex(char *buff, short size, struct HXTMTool *tool, const LONG time)
  297. {
  298.     struct StringEvent *event;
  299.     struct String *string;
  300.  
  301.     event = (struct StringEvent *) (*functions->allocevent)();
  302.      if (event)
  303.       {
  304.     string = (struct String *) (*functions->myalloc)(size + 3,MEMF_CLEAR);
  305.     if (string)
  306.     {
  307.         event->string = string;
  308.         memcpy(string->string, buff, size);
  309.         string->length = size + 2;
  310.         event->type = EVENT_SYSX;
  311.         event->status = MIDI_SYSX;
  312.         event->time = time;
  313.         event->tool = tool->tool.next;
  314.         WaitTOF();
  315.         (*functions->qevent)(event);
  316.         (*functions->myfree)(string);
  317.     }
  318.       }
  319.  
  320. }
  321.  
  322. static void __saveds trans_hx(struct HXTMTool *tool, const LONG time)
  323. {
  324.   LONG size;
  325.   int a, b, x, y ;
  326.   UBYTE abuf[32];
  327.   char chr[4];
  328.   char *zbuf = NULL;
  329.   struct Event *sysx_event;
  330.  
  331.     //translate sysexbuffer into hex
  332.  
  333.   size = strlen(sysexbuffer);
  334.   zbuf = (char *)AllocVec(size+2, MEMF_ANY|MEMF_CLEAR);
  335.   if(zbuf)
  336.      {
  337.        CopyMem(&sysexbuffer, zbuf, size);
  338.        b=0;
  339.        for(a=0;a < size;a=a+2)
  340.        {
  341.          chr[0] = zbuf[a];
  342.          chr[1] = zbuf[a+1];
  343.  
  344.          x=(isdigit((int)chr[0]))? (chr[0]&0xF) : ((chr[0]&0xDF)-55); //this is the
  345.          y=(isdigit((int)chr[1]))? (chr[1]&0xF) : ((chr[1]&0xDF)-55); //the translate-core
  346.  
  347.          abuf[b++] = ((x<<4)|y);   //writing with bitmanipulation
  348.         }
  349.       //send hexdata
  350.  
  351.        FreeVec(zbuf);
  352.        sendsysex(abuf, b, tool, time);
  353.  
  354.      }
  355.  
  356. }
  357.  
  358. static void __saveds edittoolcode(struct HXTMTool *tool)
  359. {
  360.  
  361.   struct IntuiMessage *message;
  362.   struct Window *window;
  363.   LONG class, code;
  364.   struct Gadget *gadget;
  365.   struct NewWindow *newwindow;
  366.   LONG time;
  367.  
  368.  
  369.   HXTMNewWindowStructure1.Screen = functions->screen;
  370.  
  371.   if (tool->tool.touched & TOUCH_EDIT)
  372.     {
  373.       HXTMNewWindowStructure1.LeftEdge = tool->tool.left;
  374.       HXTMNewWindowStructure1.TopEdge = tool->tool.top;
  375.       HXTMNewWindowStructure1.Width = tool->tool.width;
  376.       HXTMNewWindowStructure1.Height = tool->tool.height;
  377.     }
  378.   
  379.   if (!tool->tool.touched)
  380.     {
  381.       HXTM_tool_init(tool);
  382.     }
  383.  
  384.   newwindow = (struct NewWindow *) (*functions->DupeNewWindow)(&HXTMNewWindowStructure1);
  385.   if (!newwindow)
  386.     {
  387.       return;
  388.     }
  389.   newwindow->Title = 0;
  390.   newwindow->Flags |= BORDERLESS;
  391.   newwindow->Flags &= ~0xF;
  392.   newwindow->BlockPen = 0;
  393.   newwindow->DetailPen = 0;
  394.  
  395.   window = (struct Window *) (*functions->FlashyOpenWindow)(newwindow);
  396.   if (!window)
  397.     {
  398.       return;
  399.     }
  400.  
  401.   tool->tool.window = window;
  402.   (*functions->EmbossWindowOn)(window,WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG, HXTM_NAME,(short)-1,(short)-1,0,0);
  403.  
  404.  
  405.   for (;;)
  406.     {
  407.       ActivateGadget(&HXSTRGadget, window, NULL);
  408.       message = (struct IntuiMessage *) (*functions->GetIntuiMessage)(window);
  409.       class = message->Class;
  410.       code = message->Code;
  411.       gadget = (struct Gadget *) message->IAddress;
  412.       class = (*functions->SystemGadgets)(window,class,gadget,code);
  413.  
  414.       ReplyMsg((struct Message *)message);
  415.       if (class == CLOSEWINDOW) break;
  416.  
  417.       else if (class == GADGETUP)
  418.     {
  419.       const short id = gadget->GadgetID;
  420.       switch (id)
  421.        {
  422.            case HXSTR_GADGET:
  423.            {
  424.         time = functions->timenow;
  425.         trans_hx(tool, time);
  426.         break;
  427.            }
  428.        }
  429.      send_note(tool, time + 200, 60);
  430.     }
  431.       else if (class == GADGETDOWN)
  432.       {
  433.       }
  434.     }
  435.   tool->tool.window = 0;
  436.   tool->tool.left = window->LeftEdge;
  437.   tool->tool.top = window->TopEdge;
  438.   tool->tool.width = window->Width;
  439.   tool->tool.height = window->Height;
  440.   tool->tool.touched = TOUCH_INIT | TOUCH_EDIT;
  441.  
  442.   
  443.   (*functions->EmbossWindowOff)(window);
  444.   (*functions->FlashyCloseWindow)(window);
  445.   (*functions->DeleteNewWindow)(newwindow);
  446. }
  447. //to leave the tool correct
  448.  
  449. void __saveds removetool()
  450. {
  451.     FreeVec(HXTM);
  452. }
  453.  
  454. struct ToolMaster * inittoolmaster()
  455. {
  456.   if (!(HXTM=(UWORD *)AllocVec(sizeof (HXTMdata),MEMF_CHIP|MEMF_CLEAR))) return(NULL);
  457.   CopyMem(&HXTMdata,HXTM, sizeof (HXTMdata));
  458.  
  459.   HXTM_image.ImageData = HXTM;
  460.   memset((char *)&master,0,sizeof(struct ToolMaster));
  461.   master.toolid = HXTM_ID;
  462.   master.image = &HXTM_image;
  463.   strcpy(master.name, HXTM_NAME);
  464.   master.edittool = edittoolcode;
  465.   master.processevent = processeventcode;
  466.   master.tooltype = HXTM_TYPE;
  467.   master.toolsize = sizeof(struct HXTMTool);
  468.   master.removetool = removetool;
  469.   return(&master);
  470. }
  471.